package com.igroupes.ovineherd.model.Style;

import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.enums.FormKey;
import com.igroupes.ovineherd.util.DateUtils;
import org.apache.commons.lang3.StringUtils;

public class MonthStyle extends DateStyle {
    public MonthStyle(String label, Object value, Boolean required, String minDate, String maxDate) {
        this(label, value, required);
        this.minDate = minDate;
        this.maxDate = maxDate;
    }

    public MonthStyle(String label, Object value, Boolean required) {
        super(label, value, required);
        this.type = FormKey.MONTH.getCode();
        this.format = "YYYY-MM";
        this.inputFormat = "YYYY-MM";
    }

    public MonthStyle() {

    }

    @Override
    public Boolean verify(Object o) {
        if(!(o instanceof TimeStyle)){
            throw new ModelException("校验style类类型错误，time");
        }
        TimeStyle style = (TimeStyle) o;
        if(!style.getDisabled()){
            super.verify(o);
            if (!(style.getRequired() instanceof Boolean)){
                throw new ModelException("required格式错误");
            }
            String value = (String) style.getValue();
            if(style.getRequired() ){
                if(StringUtils.isBlank(value)){
                    throw new ModelException(style.getLabel()+"值是必传字段");
                }
            }
            if(StringUtils.isNotBlank(value)){
                if(!(style.getValue() instanceof String)){
                    throw new ModelException(style.getLabel()+"值必须是字符串");
                }

                if(!DateUtils.isValidDate(value, DateUtils.MONTH_FORMAT)){
                    throw new ModelException(style.getLabel()+"时间格式不正确,yyyy-MM");
                }
            }
        }
        return true;
    }
}
